var beginTime = "";
var endTime = "";
var storeId = $.util.getRequest("storeId");
var billId = $.util.getRequest("billId");
$(function () {
    // 填充商家基本情况数据
    $.util.get($.util.getRequestJson('/json/storeManage.storeDetail.json'),{id:storeId},function(data){
        $('#billItemList').form('load', data.data);
        $("#license").attr('href',"../"+data.data.license);
        //$.util.getRequestJson("/"+data.data.license)
    });
    //填充账单基本情况数据
    $.util.get($.util.getRequestJson('/json/consumeManage.billDetail.json'),{id:billId},function(data){
        //-1.删除；0.待确认；1.已确认；2.已结款
        var statusArray = ['待确认','已确认','已结款'];
        if(data.data.status == -1){
            data.data.status = '已删除';
        }else{
            data.data.status = statusArray[data.data.status];
        }
        if(data.data.confirmTime == null) data.data.confirmTime = '商家未确认账单';
        if(data.data.confirmContext == null) data.data.confirmContext = '--';
        $('#billItemList').form('load', data.data);
        beginTime = data.data.beginTime;
        endTime = data.data.endTime;
        initDatagrid(beginTime,endTime,billId);
    });
})

// 表格内容初始化
function initDatagrid(beginTime,endTime,billId) {
    var url = $.util.getRequestJson('/json/consumeManage.orderList.json');
    $('#billItems').datagrid({
        url: url /*+ '?type=0'*/,
        idField:'id',
        queryParams:{ beginTime:beginTime,
            endTime:endTime,
            billId:billId

        },
        fit: true,
        pagination: true,
        pageSize: 30,
        rownumbers:true,
        checkOnSelect: false,
        selectOnCheck: false,
        singleSelect: true,
        fitColumns: true,
        columns: [
            [
                {
                    field: 'expendTime',
                    title: '消费时间',
                    width:15
                },
                {
                    field: 'name',
                    title: '消费人',
                    width:15
                },
                {
                    field: 'type',
                    title: '消费类型',
                    width:15,
                    formatter:function (value, row, index) {
                        return value == 2 ? "积分消费":"商品消费";
                    }
                },{
                    field: 'expendScore',
                    title: '积分',
                    width:15
                },{
                    field: 'context',
                    width:20,
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row) {
                        if(row.type == 2){
                            var aEl = '——';
                        }else{
                            var aEl = '<a href=javascript:orderDetail("{0}"); class="easyui-tooltip" title="详情">详情</a>';
                        }
                        return $.string.format(aEl,row.id);
                    }
                }
            ]
        ]
    });
}

// 查看订单详情
function orderDetail(billId) {
    var title = "订单详情";
    var url = $.util.getRootPath() + '/page/operatorManager/billManager/orderDetail.html?billId='+billId;
    var dialog = $.easyui.showDialog({
        iconCls:"",
        title: title,
        width: 700,
        height: 600,
        maximizable: true,
        href: url,
        topMost: true,
        saveButtonText: '确认',
        onSave: function () {
            dialog.dialog('close');
        }
    });
}

// 导出所有数据
function exportExlAll() {
    $.util.get($.util.getRequestJson('/json/consumeManage.downloadData.json'),{
        beginTime:beginTime,
        endTime:endTime,
        billId:billId
    },function(data){
        var $a = $("<a></a>").attr("href", data.data).attr("download","data.xlsx");
        $a[0].click();
    });
}